/**
 * 
 */
package com.baidu.yun.push.model;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.baidu.yun.push.auth.PushKeyPair;
import com.baidu.yun.push.client.BaiduPushClient;
import com.baidu.yun.push.constants.BaiduPushConstants;
import com.baidu.yun.push.exception.PushClientException;
import com.baidu.yun.push.exception.PushServerException;
import com.icbc.cc.api.ApiCommonUtils;
import com.icbc.cte.base.CTEConstance;
import com.icbc.cte.base.TranFailException;
import com.icbc.cte.logging.Log;
import com.icbc.cte.logging.LogFactory;
import com.icbc.ctp.core.IContext;
import com.icbc.ctp.core.annotations.Input;
import com.icbc.ctp.core.annotations.JavaStep;
import com.icbc.ctp.core.annotations.Output;
import com.icbc.ctp.core.impl.OperationStep;

/**
 * TODO 类的描述：。 
 * <pre>
 *
 * </pre>
 *
 * <pre>
 * modify by Jiahy on 2016-6-17
 *    fix->1.
 *         2.
 * </pre> 
 */
@Input(name = { "msgTitle","msgCont","chanelId","divType","noticeFlag","managerId","orderId","msgType" }, type = {  "String", "String", "String", "String", "String", "String", "String", "String"}, description = { "","","", "","", "", "", "" }, mustInput = {true, true,true, true,true,true}, isVariable = {true, true, true, true, true, true })
@Output(name = { "resultmsgId"}, type = { "String"}, description = { ""})
@JavaStep
public class SinglePushMsg extends OperationStep{
	
	private static Log log = LogFactory.getCtpLog(SinglePushMsg.class);
	
	@Override
	protected int execute(IContext context)  {
		JGPushUtil pushUtil=new JGPushUtil();
		try {
		String msgCont = (String) getInputValue(context, "msgCont");
		String chanelId = (String) getInputValue(context, "chanelId");
		int divType = Integer.parseInt((String)getInputValue(context, "divType")) ;
		String msgTitle = (String) getInputValue(context, "msgTitle");
		String msgType = (String) getInputValue(context, "msgType");
		String noticeFlag = (String) getInputValue(context, "noticeFlag");
		String managerId = (String) getInputValue(context, "managerId");
		String orderId = (String) getInputValue(context, "orderId");
		
		long pushID=System.currentTimeMillis()/1000;
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String pushTime=sdf.format(new Date());
		JSONObject content = new JSONObject(); 
		JSONObject custom_content = new JSONObject(); 
		JSONObject aps = new JSONObject(); 
		try {
			aps.put("sound", "default");
			//custom_content.put("pushID", pushID);
			//custom_content.put("pushTime", pushTime);
			custom_content.put("type", "1");
			custom_content.put("msgType", msgType);
			//custom_content.put("msgUrl", "");
			//custom_content.put("chanelId", chanelId);
			custom_content.put("noticeFlag", noticeFlag);
			//custom_content.put("orderID", "1911");
			content.put("aps", aps);
			content.put("msgTitle", msgTitle);
			//消息内容
			content.put("msgCont", msgCont);
			//消息标题
			content.put("title", msgTitle);
			content.put("description", msgCont);
			content.put("custom_content", custom_content);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String Msg=content.toString();
		System.err.println(Msg);
		String msgId="";
		if(divType==1){//IOS
			divType=4;
		}else if(divType==2){//安卓
			divType=3;
		}
		//msgIds= pushSingleMessage(content,1,divType,chanelId);
		//msgId= pushSingleMessage(Msg,1,divType,chanelId);
		msgId=pushUtil.sendToAlias( chanelId,msgCont, msgCont, msgCont, custom_content.toString());
		
		setOutputValue(context, "resultmsgId", msgId);
//		setOutputValue(context, "resultmsgTitle", msgTitle);
//		setOutputValue(context, "resultmsgCont", msgCont);
//		setOutputValue(context, "resultpushTime", pushTime);
//		setOutputValue(context, "resultmsgType", "1");
//		setOutputValue(context, "resultdivType", divType);
		if(msgId !=null && !"".equals(msgId)){
			JSONObject obj=new JSONObject();
			try {
				obj.put("resultmsgId", msgId);
				obj.put("resultmsgTitle", msgTitle);
				obj.put("resultmsgCont", msgCont);
				obj.put("resultpushTime", pushTime);
				obj.put("resultmsgType", "1");
				obj.put("resultdivType", divType);
				obj.put("resultmanagerId", managerId);
				obj.put("resultorderId", orderId);
				String 	result = ApiCommonUtils.excuteOp(obj.toString(), "public_stor_msg");
				log.info("retMsg result: 参数:"+obj+"结果:"+result);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		} catch (TranFailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	/**
	* 初始化百度推送
	* @return
	*/
	public static BaiduPushClient initPushClient(String apiKey,String secretKey ){

		PushKeyPair pair = new PushKeyPair(apiKey,secretKey);
	
		BaiduPushClient pushClient = new BaiduPushClient(pair,BaiduPushConstants.CHANNEL_REST_URL);
		pushClient.setChannelLogHandler(new YunLogHandler() {
		
			@Override
			public void onHandle(YunLogEvent arg0) {
			System.out.println(arg0.getMessage());
			}
			});
		
			return pushClient;
			}
	
	//单播
	public static String pushSingleMessage(String content,int pushType,int divType,String channelId){
		String apiKey="";
		String secretKey="";
		String deployStatus=null;
		//根据操作系统不同获取不同的key
		if(divType==4){
			deployStatus= CTEConstance.getEnvProperty("deployStatus");
			apiKey = CTEConstance.getEnvProperty("apiKeyIos");
			secretKey = CTEConstance.getEnvProperty("secretKeyIos"); 
		}else if(divType==3){
			apiKey = CTEConstance.getEnvProperty("apiKey");
			secretKey = CTEConstance.getEnvProperty("secretKey"); 
		}
		//初始化推送对象
		BaiduPushClient client=initPushClient(apiKey,secretKey );
		
		PushMsgToSingleDeviceRequest request=new PushMsgToSingleDeviceRequest();
		//devType 1:web 2:pc 3:android 4:ios 5:wp
		request.setDeviceType(divType);
		
		request.setChannelId(channelId);
		
		request.setMessage(content);
		//pushType 0-置送消息 1-置送通知
		request.setMessageType(pushType);
		if(divType==4){
			request.setDeployStatus(Integer.parseInt(deployStatus));//ios推送状态    1-开发 2-生产  默认null
		}
		String msgId="";
		try {
			PushMsgToSingleDeviceResponse response=client.pushMsgToSingleDevice(request);
			msgId= response.getMsgId();
			log.info("msgId: " + response.getMsgId()+ ",sendTime: " + response.getSendTime());
		} catch (PushClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PushServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return msgId;

}
}
